/**
 * Created by dragon on 2015/12/3.
 */
require.config({
    baseUrl:PUBLIC + "/Admin/js/lib"
});
define(function(require){
    var $ = require('jquery');
    require('jquery.form');
    require('tips/msgbox');
    window.$ = window.jQuery = $;
    var c=require('../common');
    require('select');

    require('areaB');
    require('distinct');
    
    $(function(){
        c.init();

        var areaSelect = null;
        $('#area').areaB({type:1,name:'area',input:"#area_path",cutstr:18,default_val:'请选择地区',callback:function () {
            setLocation();
        }},function () {
            areaSelect = this;
        });

        var toUnicode = function (str) {
            return escape(str).replace(/%/g,"\\").toLowerCase();
        }

        //设置地图
        if($('#dituContent').length){
            var setLocation = function () {
                var areacode = $('#area_path').val();
                var address = $('#b_address').val();
                if(empty(areacode) || empty(address)) return false;

                var area = $('#province_input').html() + $('#city_input').html() + $('#area_input').html();
                address = area + address;

                $.get(URL+'/getLocation',{'address':address},function (data) {
                    if(data.status==1){
                        $('#map').val(data.data.lng+','+data.data.lat);
                        bdmap.init();
                        createBeautyMap();
                    } else {
                        c.error(data.info,2000);
                    }
                },'json');
            };

            var bdmap=null;
            var createBeautyMap = null;
            require(['creatmap'],function () {
                createBeautyMap = function () {
                    $('#dituContent').createmap({input:'#map',fn:function (address) {
                        $('#b_address').val(address.street + address.streetNumber);
                        var codeKey = address.province.substring(0,2) + '-' + address.city.substring(0,2) + '-' + address.district.substring(0,2);
                        codeKey = toUnicode(codeKey);
                        eval("var areacode = distinct[\""+codeKey+"\"]");

                        if(empty(areacode)){
                            $('#area_path').val('');
                        } else {
                            $('#area_path').val(areacode);
                        }

                        areaSelect.init();
                        //440304
                    }},function(t){
                        bdmap=t;
                    });
                };
                createBeautyMap();
            });

            //详细地址失去焦点事件
            $('#b_address').off().on('blur',function () {
                setLocation();
            });
        };
    });
});